#include "./sprite.h"

#include "../api/ex_math.h"
#include "../tools/res.h"
#include "../tools/draw.h"

//运行精灵
void sRunSpr(sSpr*spr)
{
 if(spr==NULL)
 	return;
 sAct*act = &(spr->act[spr->ind]);
 act->ind++;
 act->ind = 
 act->ind%act->pic_num;
}

//绘制精灵
int sDrawSpr(sSpr*spr,int x2,int y2)
{
 if(spr==NULL)
 	return-1;
 Res*res = getRes(spr->file,tImg);
 if(res==NULL)
 	return-2;
 
 int x = x2-spr->x;
 int y = y2-spr->y;
 int32 bit = res->id;
 sAct*act = &(spr->act[spr->ind]);
 sPic*pic = &(act->pic[act->ind]);
 drawBitmapEx(bit,
  x,y,spr->w,spr->h,
  pic->px,pic->py,
  pic->pw,pic->ph);
 return 0;
}

void sSetActId(sSpr*spr,int id)
{
 if(spr==NULL)
 	return;
 spr->ind = id;
}

void sSetImgId(sSpr*spr,int id)
{
 if(spr==NULL)
 	return;
 sAct*act = 
 &(spr->act[spr->ind]);
 act->ind = id;
 act->ind = 
 act->ind%act->pic_num;
}

//精灵，绘制坐标，偏移
void sDrawSprV9(sSpr*spr,int x2,int y2,int iw,int ih,int vw,int vh)
{
 if(spr==NULL)
 	return;
 int x = x2-spr->x;
 int y = y2-spr->y;
 
 int32 bit = 
 getRes(spr->file,tImg)->id;
 sAct*act = &(spr->act[spr->ind]);
 sPic*pic = &(act->pic[act->ind]);
 
 //上一行
 drawBitmapEx(bit,
  x,y,vw,vh,
  pic->px,pic->py,vw,vh);
 drawBitmapEx(bit,
  x+vw,y,spr->w-vw*2,vh,
  pic->px+vw,pic->py,
  iw-vw*2,vh);
 drawBitmapEx(bit,
  x+spr->w-vw,y,vw,vh,
  pic->px+iw-vw,pic->py,
  vw,vh);
 //绘制中间
 drawBitmapEx(bit,
  x,y+vh,vw,spr->h-vh*2,
  pic->px,pic->py+vh,
  vw,ih-vh*2);
 drawBitmapEx(bit,
  x+vw,y+vh,spr->w-vw*2,spr->h-vh*2,
  pic->px+vw,pic->py+vh,
  iw-vw*2,ih-vh*2);
 drawBitmapEx(bit,
  x+spr->w-vw,y+vh,vw,spr->h-vh*2,
  pic->px+iw-vw,pic->py+vh,
  vw,ih-vh*2);
 //下一行
 drawBitmapEx(bit,
  x,y+spr->h-vh,vw,vh,
  pic->px,pic->py+ih-vh,
  vw,vh);
 drawBitmapEx(bit,
  x+vw,y+spr->h-vh,spr->w-vw*2,vh,
  pic->px+vw,pic->py+ih-vh,
  iw-vw*2,vh);
 drawBitmapEx(bit,
  x+spr->w-vw,y+spr->h-vh,vw,vh,
  pic->px+iw-vw,pic->py+ih-vh,
  vw,vh);
}

//设置切片
void sSetPicJson(sPic*pic,
 cJSON*json,int len)
{
 sPic*pi = pic;
 cJSON*temp[10];
 for(int i = 0;i<len;i++)
 {
  //获取切片
  temp[0] = 
  cJSON_GetArrayItem(json,i);
  //保存切片
  temp[1] = cJSON_GetObjectItem(
   temp[0],"px");
  pi->px = temp[1]->valueint;
  temp[1] = cJSON_GetObjectItem(
   temp[0],"py");
  pi->py = temp[1]->valueint;
  temp[1] = cJSON_GetObjectItem(
   temp[0],"pw");
  pi->pw = temp[1]->valueint;
  temp[1] = cJSON_GetObjectItem(
   temp[0],"ph");
  pi->ph = temp[1]->valueint;
  pi++;
 }
}


//设置动作
void sSetActJson(sAct*act,
 cJSON*json,int len)
{
 int num;
 //char*name;
 sAct*ac = act;
 cJSON*temp[10];
 for(int i = 0;i<len;i++)
 {
  //获取动作
  temp[0] = 
  cJSON_GetArrayItem(json,i);
  //获取切片
  temp[1] = 
  cJSON_GetObjectItem(temp[0],
   "picture");
  num = 
  cJSON_GetArraySize(temp[1]);
  //载入切片
  sSetPicJson(&(ac->pic[0]),
   temp[1],num);
  //保存动作
  ac->pic_num = num;
  ac->ind = 0;
  /*
  temp[2] = 
  cJSON_GetObjectItem(temp[0],
   "name");
  name = temp[2]->valuestring;
  ac->name = (char*)
  getRes(name,tStr)->id;*/
  ac++;
 }
}

sSpr*sLoadSprFile(char*path)
{
 int len = 0,num = 0;
 char*file;
 cJSON*temp[10];
 sSpr*spr = malloc(sizeof(sSpr));
 //读取精灵文件
 cJSON*sprJson = (cJSON*)
 getRes(path,tJsn)->id;
 //解析精灵文件 
 if(sprJson)
 {
  //读取宽高
  temp[0] = 
  cJSON_GetObjectItem(sprJson,
   "w");
  spr->w = temp[0]->valueint;
  temp[0] = 
  cJSON_GetObjectItem(sprJson,
   "h");
  spr->h = temp[0]->valueint;
  temp[0] = 
  cJSON_GetObjectItem(sprJson,
   "x");
  spr->x = temp[0]->valueint;
  temp[0] = 
  cJSON_GetObjectItem(sprJson,
   "y");
  spr->y = temp[0]->valueint;
  temp[0] = 
  cJSON_GetObjectItem(sprJson,
   "file");
  file = temp[0]->valuestring;
  spr->file = (char*)
  getRes(file,tStr)->path;
  //读取动作
  temp[0] = 
  cJSON_GetObjectItem(sprJson,
   "action");
  num = 
  cJSON_GetArraySize(temp[0]);
  len = cJSON_GetArraySize(temp[0]);
  sSetActJson(&(spr->act[0]),
   temp[0],len);
 }
 spr->ind = 0;
 spr->xca_file = NULL;
 spr->yca_file = NULL;
 spr->cca_file = NULL;
 spr->act_num = num;
 return spr;
}


//打印spr
char*sPrintSpr(sSpr*spr)
{
 //总json
 cJSON*jSpr = 
 cJSON_CreateObject();
 //基础属性
 cJSON_AddStringToObject(jSpr,
  "file",spr->file);
 cJSON_AddNumberToObject(jSpr,
  "w",spr->w);
 cJSON_AddNumberToObject(jSpr,
  "h",spr->h);
 cJSON_AddNumberToObject(jSpr,
  "x",spr->x);
 cJSON_AddNumberToObject(jSpr,
  "y",spr->y);
 
 sAct*act;
 //act数组
 cJSON*aArr = 
 cJSON_CreateArray();
 for(int i = 0;i<spr->act_num;i++)
 {
  act = &(spr->act[i]);
  cJSON*aObj = 
  cJSON_CreateObject();
  
  //pic数组
  cJSON*pArr = 
  cJSON_CreateArray();
  for(int j = 0;
    j<act->pic_num;j++)
  {
   cJSON*pObj = 
   cJSON_CreateObject();
   //循环添加pic
   sPic*pic = &(act->pic[j]);
   cJSON_AddNumberToObject(pObj,
    "pw",pic->pw);
   cJSON_AddNumberToObject(pObj,
    "ph",pic->ph);
   cJSON_AddNumberToObject(pObj,
    "px",pic->px);
   cJSON_AddNumberToObject(pObj,
    "py",pic->py);
   cJSON_AddItemToArray(pArr,
    pObj);
  }
  
  cJSON_AddStringToObject(aObj,
   "name","");
  cJSON_AddItemToObject(aObj,
   "picture",pArr);
  cJSON_AddItemToArray(aArr,
   aObj);
 }
 cJSON_AddItemToObject(jSpr,
  "action",aArr);
 return cJSON_Print(jSpr);
}

//保存spr
void sSaveSpr(sSpr*spr,char*file)
{
 int32 sFile = open(file,4|8);
 char*data = sPrintSpr(spr);
 write(sFile,data,strlen(data));
 close(sFile);
}


